﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Xml.Linq;

namespace FortnoxAPI
{
    /// <summary>
    /// Get Financialyear from Fortnox
    /// </summary>
    public partial class GetFinancialyear
    {

        /// <summary>
        /// Gets the values.
        /// </summary>
        /// <param name="url">The URL.</param>
        /// <param name="token">The token.</param>
        /// <param name="db">The db.</param>
        /// <returns></returns>
        public static DataTable GetValues(string url, string token, string db)
        {
            XDocument doc = XDocument.Load(url + "token=" + token + "&db=" + db);

            DataTable dt1 = new DataTable();
            dt1.Columns.Add("ID", typeof(string));
            dt1.Columns.Add("Fromdate", typeof(string));
            dt1.Columns.Add("Todate", typeof(string));

            IEnumerable<XElement> Posts = from post in doc.Descendants("financialyear")

                                          //where (string)post.Attribute("id").Value == "1"
                                          select post;

            foreach (XElement Post in Posts)
            {
                DataRow dr1 = dt1.NewRow();

                if (Post.Element("id") != null)
                {
                    dr1["ID"] = Post.Element("id").Value;
                }
                else
                {
                    dr1["ID"] = "";
                }

                if (Post.Element("fromdate") != null)
                {
                    dr1["Fromdate"] = Post.Element("fromdate").Value;
                }
                else
                {
                    dr1["Fromdate"] = "";
                }

                if (Post.Element("todate") != null)
                {
                    dr1["Todate"] = Post.Element("todate").Value;
                }
                else
                {
                    dr1["Todate"] = "";
                }

                dt1.Rows.Add(dr1);
            }
            return dt1;

        }
    }
}
